package com.cslg.questionnaires.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class QuestionFormUtils {
    // 构造表单填写内容
    public static String buildAnswerContent(ArrayList answers){
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i < answers.size();i ++){
            HashMap<String, Object> one_answer = (HashMap)answers.get(i);
            String type = (String) one_answer.get("type");
            sb.append(type);
            sb.append("><");
            switch (type){
                case "oneChoose":
                    int oneChoose = (int)one_answer.get("answer");
                    sb.append(oneChoose);
                    break;
                case "moreChoose":
                    ArrayList moreChoose = (ArrayList)one_answer.get("answer");
                    for (int j = 0;j < moreChoose.size();j ++){
                        int one = (int)moreChoose.get(j);
                        sb.append(one);
                        sb.append("''");
                    }
                    break;
                case "oneLineText":
                case "moreLineText":
                    String answer = (String)one_answer.get("answer");
                    sb.append(answer);
                    break;
                case "recommendPerson":
                    ArrayList recommendPerson = (ArrayList)one_answer.get("answer");
                    String personMajor = (String)recommendPerson.get(0);
                    String personNum = (String) recommendPerson.get(1);
                    String personName = (String) recommendPerson.get(2);
                    sb.append(personMajor);
                    sb.append("''");
                    sb.append(personNum);
                    sb.append("''");
                    sb.append(personName);
                    break;
            }
            sb.append("<<>>");
        }
        return sb.toString();
    }

    // 解析 数据库 中表单填写内容
    public static List<HashMap> parseAnswerContent(String content){
        List<HashMap> answerContent = new ArrayList<>();
        String[] answers = content.split("<<>>");
        for(String answer : answers){
            HashMap<String,Object> oneAnswer = new HashMap<>();

            String[] type_ans = answer.split("><");
            String question_type = type_ans[0];
            oneAnswer.put("type",question_type);
            switch (question_type){
                case "oneChoose":
                    oneAnswer.put("answer",Integer.parseInt(type_ans[1]));
                    break;
                case "moreChoose":
                    String[] chooses_str = type_ans[1].split("''");
                    ArrayList<Integer> chooses_int = new ArrayList<>();
                    for (int i = 0; i < chooses_str.length; i ++){
                        chooses_int.add(Integer.parseInt(chooses_str[i]));
                    }
                    oneAnswer.put("answer",chooses_int);
                    break;
                case "oneLineText":
                case "moreLineText":
                    oneAnswer.put("answer",type_ans[1]);
                    break;
                case "recommendPerson":
                    String[] recommendPerson = type_ans[1].split("''");
                    oneAnswer.put("answer",recommendPerson);
                    break;
            }
            answerContent.add(oneAnswer);
        }
        return answerContent;
    }
}
